// store.js
import { createStore } from 'vuex'
import createPersistedState from 'vuex-persistedstate'

export default createStore({
    state: {
        userinfo: {
            username: '',
            account: '',
            password: '',
        }
    },
    mutations: {
        USER_INFO(state, info) {
            state.userinfo.username = info.Uname
            state.userinfo.account = info.UID
            state.userinfo.password = info.Password
        },
        USER_NAME(state, name) {
            state.userinfo.username = name
        },
        USER_ACCOUNT(state, acc) {
            state.userinfo.account = acc
        },
        USER_PASSWORD(state, pwd) {
            state.userinfo.password = pwd
        },
    },
    actions: {
        saveUserInfo({ commit }, data) {
            commit('USER_INFO', data)
        },
        saveUsername({ commit }, data) {
            commit('USER_NAME', data)
        },
        savePassword({ commit }, data) {
            commit('USER_PASSWORD', data)
        },
        saveAccount({ commit }, data) {
            commit('USER_ACCOUNT', data)
        },
    },
    plugins: [createPersistedState({
        storage: window.localStorage,
        reducer(state) {
            return {
                userinfo: state.userinfo
            };
        }
    })]
});


// import { createStore } from "vuex";
//
// export default createStore({
//     state:{
//         userinfo:{
//             username:'',
//             account:'',
//             password:'',
//         }
//     },
//     mutations:{
//         USER_INFO(state,info){//
//             console.log("info",info)
//             state.userinfo.username=info.Uname
//             console.log("info",info.Uname)
//             state.userinfo.account=info.UID
//             console.log("info",info.UID)
//             state.userinfo.password=info.Password
//             console.log("info",info.Password)
//         },
//         USER_NAME(state,name){
//             state.userinfo.username = name
//         },
//         USER_ACCOUNT(state,acc){
//             state.userinfo.account = acc
//         },
//         USER_PASSWORD(state,pwd){
//             state.userinfo.password = pwd
//         },
//     },
//     actions:{
//         saveUserInfo({ commit },data){
//             commit('USER_INFO',data)
//         },
//         saveUsername({ commit },data){
//             commit('USER_NAME',data)
//         },
//         savePassword({ commit },data){
//             commit('USER_PASSWORD',data)
//         },
//         saveAccount({ commit },data){
//             commit('USER_ACCOUNT',data)
//         },
//     }
// });
